home *** CD-ROM | disk | FTP | other *** search
- // Open via Map Panel.c
-
- #include "Open via Map.h"
-
- #include <MoreFilesExtras.h>
- #include <Search.h>
-
- enum
- {
- kMenuBarID = 128,
-
- kAppleMenu = 128,
- kFileMenu = 129,
- kEditMenu = 130,
-
- kAboutItem = 1,
-
- kCloseItem = 1,
- kQuitItem = 3,
-
- kUndoItem = 1,
- kCutItem = 3,
- kCopyItem = 4,
- kPasteItem = 5,
- kClearItem = 6
- };
-
- enum
- {
- kMainDlogID = 128,
- kActiveItemID = 1,
-
- kAboutDlogID = 129,
- kVersionItemID = 5
- };
-
- Boolean gQuit;
- Boolean gInBackground;
-
- MenuHandle gAppleMenu;
- MenuHandle gFileMenu;
- MenuHandle gEditMenu;
-
- Preferences gPreferences;
- Preferences gStartPreferences;
-
- static Boolean IsSystemReady(void)
- {
- OSStatus theStatus;
- long theResponse;
- Boolean isPresent;
-
- theStatus = Gestalt(gestaltAppearanceAttr, &theResponse);
-
- if (theStatus == noErr)
- isPresent = theResponse & (1 << gestaltAppearanceExists);
-
- if ((theStatus == noErr) && isPresent)
- {
- theStatus = Gestalt(gestaltWindowMgrAttr, &theResponse);
-
- if (theStatus == noErr)
- isPresent = theResponse & gestaltWindowMgrPresent;
- }
-
- if (theStatus != noErr)
- isPresent = false;
-
- return isPresent;
- }
-
- static void Initialize(void)
- {
- InitGraf(&qd.thePort);
- InitFonts();
- InitWindows();
- InitMenus();
- TEInit();
- InitDialogs(NULL);
-
- MaxApplZone();
-
- if (IsSystemReady())
- RegisterAppearanceClient();
- else
- {
- SysBeep(0);
- gQuit = true;
- }
-
- FlushEvents(everyEvent, 0);
- InitCursor();
- }
-
- static void InstallMenus(void)
- {
- Handle theMenuBar;
-
- theMenuBar = GetNewMBar(kMenuBarID);
-
- if (theMenuBar)
- {
- SetMenuBar(theMenuBar);
- DisposeHandle(theMenuBar);
-
- gAppleMenu = GetMenuHandle(kAppleMenu);
- gFileMenu = GetMenuHandle(kFileMenu);
- gEditMenu = GetMenuHandle(kEditMenu);
-
- AppendResMenu(gAppleMenu, 'DRVR');
-
- DrawMenuBar();
- }
- }
-
- static OSErr GetPreferences(void)
- {
- OSErr theErr;
- FSSpec theSpec;
- short refNum;
- Preferences **thePreferences;
-
- gPreferences.theVersion = kPreferencesVersion;
- gPreferences.isActive = true;
- gPreferences.thePosition.top = 0;
- gPreferences.thePosition.left = 0;
- gPreferences.thePosition.bottom = 0;
- gPreferences.thePosition.right = 0;
-
- theErr = FindFolder(kOnSystemDisk, kPreferencesFolderType, kDontCreateFolder,
- &theSpec.vRefNum, &theSpec.parID);
-
- if (theErr == noErr)
- {
- GetIndString(theSpec.name, kPreferencesStrxID, kPrefNameIndex);
-
- refNum = FSpOpenResFile(&theSpec, fsRdPerm);
- theErr = ResError();
-
- if (theErr == noErr)
- {
- thePreferences = (Preferences **) Get1Resource(kPreferencesType, kPreferencesID);
-
- if (thePreferences && ((**thePreferences).theVersion == kPreferencesVersion))
- gPreferences = **thePreferences;
-
- CloseResFile(refNum);
- }
- }
-
- gStartPreferences = gPreferences;
-
- return theErr;
- }
-
- static OSErr SavePreferences(void)
- {
- OSErr theErr;
- OSStatus theStatus;
- short refNum;
- WindowPtr theWindow;
- FSSpec theSpec;
- Preferences **thePreferences;
-
- theWindow = FrontWindow();
-
- theStatus = GetWindowBounds(theWindow, kWindowStructureRgn, &gPreferences.thePosition);
-
- if (theStatus != noErr)
- {
- gPreferences.thePosition.top = 0;
- gPreferences.thePosition.left = 0;
- gPreferences.thePosition.bottom = 0;
- gPreferences.thePosition.right = 0;
- }
-
- if ((gPreferences.theVersion != gStartPreferences.theVersion) ||
- (gPreferences.isActive != gStartPreferences.isActive) ||
- (gPreferences.thePosition.top != gStartPreferences.thePosition.top) ||
- (gPreferences.thePosition.left != gStartPreferences.thePosition.left))
- {
- theErr = FindFolder(kOnSystemDisk, kPreferencesFolderType, kCreateFolder,
- &theSpec.vRefNum, &theSpec.parID);
- }
- else
- {
- // The preferences are the same, so don't write them out
- // The easiest way to do this is to just set the error flag here
- theErr = 1;
- }
-
- if (theErr == noErr)
- {
- GetIndString(theSpec.name, kPreferencesStrxID, kPrefNameIndex);
-
- refNum = FSpOpenResFile(&theSpec, fsRdWrPerm);
- theErr = ResError();
-
- if (theErr == fnfErr)
- {
- FSpCreateResFile(&theSpec, kOpenViaMapPanelCode, kPreferencesType, smSystemScript);
- theErr = ResError();
-
- if (theErr == noErr)
- {
- refNum = FSpOpenResFile(&theSpec, fsRdWrPerm);
- theErr = ResError();
- }
- }
-
- if (theErr == noErr)
- {
- thePreferences = (Preferences **) Get1Resource(kPreferencesType, kPreferencesID);
-
- if (thePreferences)
- {
- RemoveResource((Handle) thePreferences);
- theErr = ResError();
-
- if (theErr == noErr)
- {
- DisposeHandle((Handle) thePreferences);
-
- UpdateResFile(refNum);
- theErr = ResError();
- }
- }
-
- thePreferences = (Preferences **) NewHandle(sizeof(gPreferences));
-
- if (thePreferences)
- {
- **thePreferences = gPreferences;
- AddResource((Handle) thePreferences, kPreferencesType, kPreferencesID, NULL);
- theErr = ResError();
-
- UpdateResFile(refNum);
- theErr = ResError();
- }
-
- CloseResFile(refNum);
- }
- }
-
- gStartPreferences = gPreferences;
-
- return theErr;
- }
-
- static Boolean FindActiveProcessID(OSType theCreator, ProcessSerialNumber *thePSN)
- {
- Boolean isFound;
- ProcessInfoRec theInfo;
- FSSpec theProcessSpec;
- Str31 theName;
-
- isFound = false;
-
- thePSN->highLongOfPSN = 0;
- thePSN->lowLongOfPSN = kNoProcess;
-
- while ((!isFound) && (GetNextProcess(thePSN) == noErr))
- {
- theInfo.processInfoLength = sizeof(theInfo);
- theInfo.processName = (StringPtr) &theName;
- theInfo.processAppSpec = &theProcessSpec;
-
- if (GetProcessInformation(thePSN, &theInfo) == noErr)
- {
- if (theCreator == theInfo.processSignature)
- isFound = true;
- }
- }
-
- if (!isFound)
- {
- thePSN->highLongOfPSN = 0;
- thePSN->lowLongOfPSN = kNoProcess;
- }
-
- return isFound;
- }
-
- static OSErr QuitBackground(void)
- {
- OSErr theErr;
- AEAddressDesc theTarget;
- AppleEvent theEvent;
- ProcessSerialNumber thePSN;
-
- FindActiveProcessID(kOpenViaMapExtensionCode, &thePSN);
-
- theErr = AECreateDesc(typeProcessSerialNumber, &thePSN, sizeof(thePSN), &theTarget);
-
- if (theErr == noErr)
- {
- theErr = AECreateAppleEvent(kCoreEventClass, kAEQuitApplication, &theTarget,
- kAutoGenerateReturnID, kAnyTransactionID, &theEvent);
-
- AEDisposeDesc(&theTarget);
-
- if (theErr == noErr)
- {
- theErr = AESend(&theEvent, NULL, kAENoReply + kAECanInteract,
- kAENormalPriority, kAEDefaultTimeout, NULL, NULL);
-
- AEDisposeDesc(&theEvent);
- }
- }
-
- return theErr;
- }
-
- static OSErr ChangeActiveBackground(Boolean isActive)
- {
- OSErr theErr;
- AEAddressDesc theTarget;
- AppleEvent theEvent;
- ProcessSerialNumber thePSN;
-
- FindActiveProcessID(kOpenViaMapExtensionCode, &thePSN);
-
- theErr = AECreateDesc(typeProcessSerialNumber, &thePSN, sizeof(thePSN), &theTarget);
-
- if (theErr == noErr)
- {
- theErr = AECreateAppleEvent(kOpenViaMapEventClass, kActiveChange, &theTarget,
- kAutoGenerateReturnID, kAnyTransactionID, &theEvent);
-
- AEDisposeDesc(&theTarget);
-
- if (theErr == noErr)
- {
- theErr = AEPutParamPtr(&theEvent, keyDirectObject, typeBoolean, &isActive,
- sizeof(isActive));
-
- if (theErr == noErr)
- theErr = AESend(&theEvent, NULL, kAENoReply + kAECanInteract,
- kAENormalPriority, kAEDefaultTimeout, NULL, NULL);
-
- AEDisposeDesc(&theEvent);
- }
- }
-
- return theErr;
- }
-
- static OSErr FindBackground(FSSpec *theSpec)
- {
- OSErr theErr;
- short theRealVRefNum;
- long theMatchCount;
-
- theErr = DetermineVRefNum(NULL, 0, &theRealVRefNum);
-
- if (theErr == noErr)
- {
- theErr = CreatorTypeFileSearch(NULL, theRealVRefNum, kOpenViaMapExtensionCode,
- kBackgroundAppCode, theSpec, 1, &theMatchCount, true);
-
- if ((theErr == noErr) || (theErr == eofErr))
- {
- if (theMatchCount <= 0)
- theErr = afpItemNotFound;
- }
- }
-
- return theErr;
- }
-
- static OSErr StartBackground(void)
- {
- OSErr theErr;
- LaunchParamBlockRec pb;
- FSSpec theAppSpec;
-
- theErr = FindBackground(&theAppSpec);
-
- if (theErr == noErr)
- {
- pb.launchBlockID = extendedBlock;
- pb.launchEPBLength = extendedBlockLen;
- pb.launchFileFlags = 0;
- pb.launchControlFlags = launchContinue + launchNoFileFlags;
- pb.launchAppSpec = &theAppSpec;
- pb.launchAppParameters = NULL;
-
- theErr = LaunchApplication(&pb);
- }
-
- return theErr;
- }
-
- static OSErr ChangeActive(DialogPtr theDialog, Boolean isActive)
- {
- OSErr theErr;
- Boolean isRunning;
- ControlHandle theControl;
- ProcessSerialNumber thePSN;
-
- theErr = GetDialogItemAsControl(theDialog, kActiveItemID, &theControl);
-
- if (theErr == noErr)
- {
- isRunning = FindActiveProcessID(kOpenViaMapExtensionCode, &thePSN);
-
- gPreferences.isActive = isActive;
-
- SetControlValue(theControl, isActive);
-
- if (isRunning)
- ChangeActiveBackground(isActive);
- else
- {
- SavePreferences();
- StartBackground();
- }
- }
-
- return theErr;
- }
-
- static void SetupWindow(void)
- {
- OSStatus theStatus;
- DialogPtr theDialog;
-
- theDialog = GetNewDialog(kMainDlogID, NULL, (WindowPtr) -1);
-
- ChangeActive(theDialog, gPreferences.isActive);
-
- if (gPreferences.thePosition.top || gPreferences.thePosition.left ||
- gPreferences.thePosition.bottom || gPreferences.thePosition.right)
- theStatus = MoveWindowStructure(GetDialogWindow(theDialog),
- gPreferences.thePosition.left, gPreferences.thePosition.top);
-
- theStatus = TransitionWindow(GetDialogWindow(theDialog), kWindowZoomTransitionEffect,
- kWindowShowTransitionAction, NULL);
- }
-
- static void DoAbout(void)
- {
- OSErr theErr;
- Boolean isDone;
- DialogItemIndex itemHit;
- DialogPtr theDialog;
- Handle theVersion;
- ControlHandle theControl;
-
- theDialog = GetNewDialog(kAboutDlogID, NULL, (WindowPtr) -1);
-
- if (theDialog)
- {
- theVersion = Get1Resource('vers', 1);
-
- if (theVersion)
- {
- theErr = GetDialogItemAsControl(theDialog, kVersionItemID, &theControl);
-
- if (theErr == noErr)
- {
- HLock(theVersion);
- SetDialogItemText((Handle) theControl, (ConstStr255Param) ((*theVersion) + 6));
- }
-
- ReleaseResource(theVersion);
- }
-
- theErr = TransitionWindow(GetDialogWindow(theDialog), kWindowZoomTransitionEffect,
- kWindowShowTransitionAction, NULL);
-
- isDone = false;
-
- while (!isDone)
- {
- ModalDialog(NULL, &itemHit);
-
- if (itemHit)
- isDone = true;
- }
-
- theErr = TransitionWindow(GetDialogWindow(theDialog), kWindowZoomTransitionEffect,
- kWindowHideTransitionAction, NULL);
-
- DisposeDialog(theDialog);
- }
- }
-
- static OSErr DoQuit(void)
- {
- gQuit = true;
-
- return noErr;
- }
-
- static OSErr GetTargetFromSelf(AEAddressDesc *theTarget)
- {
- OSErr theErr;
- ProcessSerialNumber thePSN;
-
- thePSN.highLongOfPSN = 0;
- thePSN.lowLongOfPSN = kCurrentProcess;
-
- theErr = AECreateDesc(typeProcessSerialNumber, &thePSN, sizeof(thePSN), theTarget);
-
- return theErr;
- }
-
- static OSErr DoSendQuit(void)
- {
- OSErr theErr;
- AEAddressDesc theTarget;
- AppleEvent theEvent;
-
- theErr = GetTargetFromSelf(&theTarget);
-
- if (theErr == noErr)
- {
- theErr = AECreateAppleEvent(kCoreEventClass, kAEQuitApplication, &theTarget,
- kAutoGenerateReturnID, kAnyTransactionID, &theEvent);
-
- AEDisposeDesc(&theTarget);
-
- if (theErr == noErr)
- {
- theErr = AESend(&theEvent, NULL, kAENoReply + kAECanInteract,
- kAENormalPriority, kAEDefaultTimeout, NULL, NULL);
-
- AEDisposeDesc(&theEvent);
- }
- }
-
- return theErr;
- }
-
- static OSErr GotRequiredParams(AppleEvent *theEvent)
- {
- OSErr theErr;
- DescType theType;
- Size theSize;
-
- theErr = AEGetAttributePtr(theEvent, keyMissedKeywordAttr, typeWildCard, &theType,
- NULL, 0, &theSize);
-
- if (theErr == errAEDescNotFound)
- theErr = noErr;
- else
- theErr = errAEParamMissed;
-
- return theErr;
- }
-
- static pascal OSErr DoHandleOpenApplication(AppleEvent *theEvent, AppleEvent *theReply,
- UInt32 refCon)
- {
- #pragma unused(theReply, refCon)
- OSErr theErr;
-
- theErr = GotRequiredParams(theEvent);
-
- return theErr;
- }
-
- static pascal OSErr DoHandleOpenPrint(AppleEvent *theEvent, Boolean isOpen)
- {
- #pragma unused(isOpen)
- OSErr theErr;
-
- theErr = GotRequiredParams(theEvent);
-
- return theErr;
- }
-
- static pascal OSErr DoHandleOpenDocuments(AppleEvent *theEvent, AppleEvent *theReply,
- UInt32 refCon)
- {
- #pragma unused(theReply, refCon)
- OSErr theErr;
-
- theErr = DoHandleOpenPrint(theEvent, true);
-
- return theErr;
- }
-
- static pascal OSErr DoHandlePrintDocuments(AppleEvent *theEvent, AppleEvent *theReply,
- UInt32 refCon)
- {
- #pragma unused(theReply, refCon)
- OSErr theErr;
-
- theErr = DoHandleOpenPrint(theEvent, false);
-
- return theErr;
- }
-
- static pascal OSErr DoHandleQuit(AppleEvent *theEvent, AppleEvent *theReply, UInt32 refCon)
- {
- #pragma unused(theReply, refCon)
- OSErr theErr;
-
- theErr = GotRequiredParams(theEvent);
-
- if (theErr == noErr)
- theErr = DoQuit();
-
- return theErr;
- }
-
- static void InstallEventHandlers(void)
- {
- OSErr theErr;
-
- theErr = AEInstallEventHandler(kCoreEventClass, kAEOpenApplication,
- NewAEEventHandlerProc(&DoHandleOpenApplication), 0, false);
-
- if (theErr == noErr)
- theErr = AEInstallEventHandler(kCoreEventClass, kAEOpenDocuments,
- NewAEEventHandlerProc(&DoHandleOpenDocuments), 0, false);
-
- if (theErr == noErr)
- theErr = AEInstallEventHandler(kCoreEventClass, kAEPrintDocuments,
- NewAEEventHandlerProc(&DoHandlePrintDocuments), 0, false);
-
- if (theErr == noErr)
- theErr = AEInstallEventHandler(kCoreEventClass, kAEQuitApplication,
- NewAEEventHandlerProc(&DoHandleQuit), 0, false);
- }
-
- static void DoAppleMenu(short theMenuItem)
- {
- Str255 theItemName;
-
- switch (theMenuItem)
- {
- case kAboutItem:
- DoAbout();
- break;
- default:
- GetMenuItemText(gAppleMenu, theMenuItem, theItemName);
- OpenDeskAcc(theItemName);
- break;
- }
- }
-
- static void DoFileMenu(short theMenuItem)
- {
- switch (theMenuItem)
- {
- case kCloseItem:
- DoSendQuit();
- break;
- case kQuitItem:
- DoSendQuit();
- break;
- }
- }
-
- static void DoEditMenu(short theMenuItem)
- {
- #pragma unused(theMenuItem)
- }
-
- static void DoMenuCommand(UInt32 theMenuResult)
- {
- short theMenuID;
- short theMenuItem;
-
- theMenuID = HiWord(theMenuResult);
- theMenuItem = LoWord(theMenuResult);
-
- switch (theMenuID)
- {
- case kAppleMenu:
- DoAppleMenu(theMenuItem);
- break;
- case kFileMenu:
- DoFileMenu(theMenuItem);
- break;
- case kEditMenu:
- DoEditMenu(theMenuItem);
- break;
- }
-
- HiliteMenu(0);
- }
-
- static void DoUpdate(WindowPtr theWindow)
- {
- BeginUpdate(theWindow);
- UpdateDialog(theWindow, theWindow->visRgn);
- EndUpdate(theWindow);
- }
-
- static void DoActivate(WindowPtr theWindow, Boolean isActivating)
- {
- OSErr theErr;
- ControlHandle theRoot;
-
- theErr = GetRootControl(theWindow, &theRoot);
-
- if (theErr == noErr)
- {
- if (isActivating)
- theErr = ActivateControl(theRoot);
- else
- theErr = DeactivateControl(theRoot);
- }
- }
-
- static void DoContentClick(EventRecord *theEvent, WindowPtr theWindow)
- {
- #pragma unused(theWindow)
- OSErr theErr;
- Boolean wasHit;
- SInt16 theValue;
- DialogItemIndex theItem;
- DialogPtr theDialog;
- ControlHandle theControl;
-
- wasHit = DialogSelect(theEvent, &theDialog, &theItem);
-
- if (theItem == kActiveItemID)
- {
- theErr = GetDialogItemAsControl(theDialog, theItem, &theControl);
-
- if (theErr == noErr)
- {
- theValue = GetControlValue(theControl);
-
- theErr = ChangeActive(theDialog, !theValue);
- }
- }
- }
-
- static void DoMouseDown(EventRecord *theEvent)
- {
- short thePart;
- WindowPtr theWindow;
-
- thePart = FindWindow(theEvent->where, &theWindow);
-
- switch (thePart)
- {
- case inMenuBar:
- DoMenuCommand(MenuSelect(theEvent->where));
- break;
- case inSysWindow:
- SystemClick(theEvent, theWindow);
- break;
- case inContent:
- DoContentClick(theEvent, theWindow);
- break;
- case inDrag:
- DragWindow(theWindow, theEvent->where, &(**GetGrayRgn()).rgnBBox);
- break;
- case inGrow:
- break;
- case inGoAway:
- if (TrackGoAway(theWindow, theEvent->where))
- DoSendQuit();
- break;
- case inZoomIn:
- case inZoomOut:
- break;
- }
- }
-
- static void DoKeyDown(EventRecord *theEvent)
- {
- char theKey;
-
- theKey = theEvent->message & charCodeMask;
-
- if (theEvent->modifiers & cmdKey)
- {
- DoMenuCommand(MenuEvent(theEvent));
- }
- }
-
- static void DoDiskEvent(EventRecord *theEvent)
- {
- OSErr theErr;
- Point thePoint;
-
- if (HiWord(theEvent->message) != noErr)
- {
- DILoad();
- SetPt(&thePoint, 120, 120);
- theErr = DIBadMount(thePoint, theEvent->message);
- DIUnload();
- }
- }
-
- static void DoSuspendResumeEvent(EventRecord *theEvent)
- {
- if (theEvent->message & resumeFlag)
- gInBackground = false;
- else
- gInBackground = true;
-
- DoActivate(FrontWindow(), !gInBackground);
- }
-
- static void DoOSEvent(EventRecord *theEvent)
- {
- if (((theEvent->message & osEvtMessageMask) >> 24) == suspendResumeMessage)
- DoSuspendResumeEvent(theEvent);
- }
-
- static void DoHighLevelEvent(EventRecord *theEvent)
- {
- OSErr theErr;
-
- theErr = AEProcessAppleEvent(theEvent);
- }
-
- static void DoEvent(EventRecord *theEvent)
- {
- switch (theEvent->what)
- {
- case mouseDown:
- DoMouseDown(theEvent);
- break;
- case keyDown:
- case autoKey:
- DoKeyDown(theEvent);
- break;
- case activateEvt:
- DoActivate((WindowPtr) theEvent->message, theEvent->modifiers & activeFlag);
- break;
- case updateEvt:
- DoUpdate((WindowPtr) theEvent->message);
- break;
- case diskEvt:
- DoDiskEvent(theEvent);
- break;
- case osEvt:
- DoOSEvent(theEvent);
- break;
- case kHighLevelEvent:
- DoHighLevelEvent(theEvent);
- break;
- }
- }
-
- static void DoIdle(EventRecord *theEvent)
- {
- #pragma unused(theEvent)
- }
-
- static void MainEventLoop(void)
- {
- Boolean gotEvent;
- UInt32 theSleep;
- EventRecord theEvent;
-
- theSleep = GetCaretTime();
-
- while (!gQuit)
- {
- gotEvent = WaitNextEvent(everyEvent, &theEvent, theSleep, NULL);
-
- if (gotEvent)
- DoEvent(&theEvent);
- else
- DoIdle(&theEvent);
- }
- }
-
- void main(void)
- {
- gQuit = false;
- gInBackground = false;
-
- Initialize();
-
- if (!gQuit)
- {
- InstallEventHandlers();
- InstallMenus();
- GetPreferences();
- SetupWindow();
-
- MainEventLoop();
-
- TransitionWindow(FrontWindow(), kWindowZoomTransitionEffect, kWindowHideTransitionAction,
- NULL);
-
- SavePreferences();
- }
- }